import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_AppHomeEntity.dart';
import 'package:flutter_learn_vi/src/common/global.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/home/model_home.dart';
import 'package:flutter_learn_vi/src/page/home/new/v4_home_item_bottom_widget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 3/29/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class HomeItemBottomListWidget extends StatefulWidget {
  @override
  _HomeItemBottomListWidgetState createState() =>
      _HomeItemBottomListWidgetState();
}

class _HomeItemBottomListWidgetState extends State<HomeItemBottomListWidget>
    with HomeModel {
  StreamSubscription _streamSubscription;

  @override
  void initState() {
    super.initState();
    _streamSubscription = rootStreamController.stream.listen((event) {
      if (event.code == 203) {
        _loadingHomeList();
      }
    });
    _loadingHomeList();
  }

  @override
  void dispose() {
    _streamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_list.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          setState(() {
            _isLoading = true;
          });

          Future.delayed(Duration(milliseconds: 500), () {
            _loadingHomeList();
          });
        },
      );
    }
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemCount: _list.length,
      itemBuilder: (BuildContext context, int index) {
        BeanAppHomeEntity entity = _list[index];
        return HomeItemBottomWidget(entity: entity);
      },
    );
  }

  bool _isLoading = true;
  List<BeanAppHomeEntity> _list = [];

  void _loadingHomeList() async {
    _list = await getHomeList();
    _isLoading = false;
    if (mounted) {
      setState(() {});
    }
  }
}
